/**
 * Created by Scor on 2015/8/25.
 */
//AngularJS 控制器
var grApp = angular.module('gr-app', []);
grApp.controller('AddrCtrl',['$scope',
    '$http',
    'AddressFactory',
    function($scope,$http,addressFactory){
    //页面初始化载入顺序事件
        var loadingModal = angular.element("#icon-alert");
        loadingModal.modal();
        var formModal = angular.element("#form-alert");
        var progress = $.AMUI.progress;
        $scope.value={name:"",phone:"",address:""};
        $scope.alertContent="你的信息不完整";
        addressFactory.getAddressList().then(function(data) {  // 调用承诺API获取数据 .resolve
             $scope.addrs=data;
            loadingModal.modal('close');
            $scope.backurl=angular.element('#backurl').val();
            if($scope.backurl!=""&&$scope.backurl!=" "){
                $scope.addAddressForm();
            }
        });
        //删除地址
        $scope.deleteAddress=function(index,objectId){
            loadingModal.modal();
            addressFactory.deleteAddress(objectId).then(function(data){
                $scope.addrs.splice(index,1);
                loadingModal.modal('close');
            });
        };
        //设置默认值事件
        $scope.setDefaultAddress=function(index,objectId){
            loadingModal.modal();
            addressFactory.setDefaultAddress(objectId).then(function(data){
                $scope.addrs=data;
                loadingModal.modal('close');
            });
        };
        //页面跳转
        $scope.jump=function(args){
                loadingModal.modal();
                location.href=(args);
                loadingModal.modal('close');
        };
        //新建地址表单生成
        $scope.addAddressForm=function(){
            delete $scope.value.objectId;
            $scope.formTitle="新建地址";
            //$scope.value.objectId="";
            $scope.noObjectid=false;
            $scope.value.name='';
            $scope.value.phone='';
            $scope.value.address='';
            //判断是否首次新增地址，如果是，则设为默认地址
            if($scope.addrs.length==0){
                $scope.value.isDefault=true;
            }else{
                $scope.value.isDefault=false;
            }
            formModal.modal();
        };
        //修改地址表单生成
        $scope.getEditAddress=function(index,addr){
            $scope.formTitle="修改地址";
            $scope.value.objectId=addr.objectId;
            $scope.noObjectid=true;
            $scope.value.name=addr.name;
            $scope.value.phone=addr.phone;
            $scope.value.address=addr.address;
            $scope.value.isDefault=addr.isDefault;
            formModal.modal();
        };
        //新建地址检查提交
        $scope.saveAddress=function(){
            if($scope.value.name==""){
                return false;
            }
            if(isNaN($scope.value.phone)||$scope.value.phone==""){
                return false;
            }
            if($scope.value.address==""||$scope.value.address==null){
                return false;
            }
            formModal.modal('close');
            loadingModal.modal();
            addressFactory.saveAddress($scope.value).then(function(data){
                if($scope.backurl!=""&&$scope.backurl!=" "){
                    location.href=$scope.backurl+data.objectId;
                    return false;
                }
                $scope.newAddressFlag=true;
                var newAddress=data;
                //判断是否新增
                angular.forEach($scope.addrs,function(data,index,array){
                    if(data.objectId==newAddress.objectId){
                        $scope.addrs[index]=newAddress;
                        $scope.newAddressFlag=false;
                    }
                });
                if($scope.newAddressFlag){
                    $scope.addrs.push(newAddress);
                }
                loadingModal.modal('close');
            });
    };

}]);

//创建工厂/Service
grApp.factory('AddressFactory',['$http', '$q', function ($http, $q){
    var service={};
    //获取地址列表方法
    service.getAddressList= function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/user/center/getAddress'
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    //删除地址方法
    service.deleteAddress= function (objectId) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        var obj=objectId;
        $http({
            method: 'GET',
            url: '/user/center/deleteAddress',
            params:{objectId:obj}
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    //设置默认地址方法
    service.setDefaultAddress= function (objectId) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/user/center/setDefaultAddress',
            params:{objectId:objectId}
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    //编辑修改地址
    service.saveAddress= function (addr) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'post',
            url: '/user/center/saveAddress',
            params:addr
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    //弹出提示框方法
    service.warningAlert= function (target) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        target.modal({
            relatedTarget: this,
            onConfirm: function() {
                return false;
            }
        });
    };

    return service;
}]);